/* 
 * File:   DocumentManager.cpp
 * Author: taylor
 * 
 * Created on November 7, 2012, 2:27 PM
 */

#include "DocumentManager.h"

DocumentManager::DocumentManager(char type){
    _terms = IndexHandler(type);
}

map<int, Document>* DocumentManager::getDocuments() {
    return &_documents;
}

IndexHandler* DocumentManager::getTerms() {
    return &_terms;
}

Document DocumentManager::getDocument(const int docID) {
    return _documents.find(docID)->second;
}

bool DocumentManager::documentExists(int docID) {
    if (_documents.count(docID) > 0)
        return true;
    return false;
}

void DocumentManager::addDocument(int id, Document document) {
    if (!documentExists(id)) {
        pair<int, Document> data(id, document);
        _documents.insert(data);
    }
}

void DocumentManager::readStopWordList(string filename = "StopWordsList.txt") {
    //string filename = "StopWordsList.txt";
    fstream fin;

    fin.open(filename.c_str(), fstream::in);
    if (fin) {
        string str;
        while (fin >> str) {
            _stopWordsList.push_back(str);
        }
    } else
        cout << "Stop word file could not be opened.";
    fin.close();
}

void DocumentManager::addDocumentsFromFile(string fileName) {
    ifstream file(fileName);
    xml_document<> doc;
    DocumentParser dp;
    
    if(!file){
        cout << "Error reading file\n\n";
        return;
    }

    //Create char buffer and add XML document to string for XML parsing
    vector<char> buffer((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
    buffer.push_back('\0');
    doc.parse<0>(&buffer[0]);
    //End buffer

    xml_node<> *node = doc.first_node()->first_node();
    xml_node<> *title = doc.first_node()->first_node();

    while (node != 0) {

        vector<string> aInfo;
        string info;
        string dTitle;

        xml_node<> *name = title->first_node("title");
        xml_node<> *text = node->first_node("revision")->first_node("text");
        xml_node<> *id = node->first_node("revision")->first_node("id");

        dTitle = name->value();
        info = text->value();
        int dId = atoi(id->value());

        node = node->next_sibling();
        title = title->next_sibling();


        Document d(dId, dTitle);

        dp.breakIntoTokens(info, aInfo);
        dp.parseInfo(aInfo);


        //d.setTerms(aInfo);
        for (int i = 0; i < aInfo.size(); i++) {

            Term* term = _terms.insert(aInfo.at(i));
            term->addOccuranceInDocument(dId);

            cout << aInfo.at(i) << endl;
        }

        //insert document into the map
        addDocument(dId, d);
    }
}
